﻿using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;

namespace EagleJournal
{
	public partial class CharmMessageBox : UserControl
    {
        public static readonly  DependencyProperty
            MessageTitleProperty        = DependencyProperty.Register("MessageTitle", typeof(string), typeof(CharmMessageBox));
        public                  string
            MessageTitle
        {
            get { return (string)this.GetValue(MessageTitleProperty); }
            set { this.SetValue(MessageTitleProperty, value); }
        }

        public static readonly  DependencyProperty
            MessageContentProperty      = DependencyProperty.Register("MessageContent", typeof(string), typeof(CharmMessageBox));
        public                  string
            MessageContent
        {
            get { return (string)this.GetValue(MessageContentProperty); }
            set { this.SetValue(MessageContentProperty, value); }
        }

        public static readonly  DependencyProperty
            MessageImageProperty        = DependencyProperty.Register("MessageImage", typeof(MessageBoxImage), typeof(CharmMessageBox));
        public                  MessageBoxImage
            MessageImage
        {
            get { return (MessageBoxImage)this.GetValue(MessageImageProperty); }
            set { this.SetValue(MessageImageProperty, value); }
        }

        public static readonly  DependencyProperty
            MessageButtonsProperty      = DependencyProperty.Register("MessageButtons", typeof(MessageBoxButton), typeof(CharmMessageBox));
        public                  MessageBoxButton
            MessageButtons
        {
            get { return (MessageBoxButton)this.GetValue(MessageButtonsProperty); }
            set { this.SetValue(MessageButtonsProperty, value); }
        }



        public static readonly  RoutedEvent
            InvokeMessageEvent          = EventManager.RegisterRoutedEvent("InvokeMessage", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CharmMessageBox));
        public event            RoutedEventHandler
            InvokeMessage
        {
            add     { this.AddHandler(InvokeMessageEvent, value); }
            remove  { this.RemoveHandler(InvokeMessageEvent, value); }
        }

        public static readonly  RoutedEvent
            InvokeDialogConfirmEvent    = EventManager.RegisterRoutedEvent("InvokeDialogConfirm", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CharmMessageBox));
        public event            RoutedEventHandler
            InvokeDialogConfirm
        {
            add     { this.AddHandler(InvokeDialogConfirmEvent, value); }
            remove  { this.RemoveHandler(InvokeDialogConfirmEvent, value); }
        }

		public          CharmMessageBox                 ()
		{
			this.InitializeComponent();
		}

        private void    UserControl_IsVisibleChanged    (object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.Visibility == Visibility.Hidden)
            {
                this.info.Visibility = Visibility.Collapsed;
                this.stop.Visibility = Visibility.Collapsed;
                this.warn.Visibility = Visibility.Collapsed;
                this.help.Visibility = Visibility.Collapsed;

                this.ButtonOk.Visibility        = Visibility.Collapsed;
                this.ButtonCancel.Visibility    = Visibility.Collapsed;

                this.ButtonYes.Visibility       = Visibility.Collapsed;
                this.ButtonNo.Visibility        = Visibility.Collapsed;
            }           
        }

        public  void    ShowMessage                     (string title, string message, MessageBoxImage image, MessageBoxButton buttons)
        {
            this.MessageTitle   = title;
            this.MessageContent = message;

            this.MessageImage   = image;
            this.MessageButtons = buttons;

            this.info.Visibility = Visibility.Collapsed;
            this.warn.Visibility = Visibility.Collapsed;
            this.help.Visibility = Visibility.Collapsed;
            this.stop.Visibility = Visibility.Collapsed;

            switch (this.MessageImage)
            {
                case MessageBoxImage.Information:
                    this.info.Visibility = Visibility.Visible;
                    break;
                case MessageBoxImage.Warning:
                    this.warn.Visibility = Visibility.Visible;
                    break;
                case MessageBoxImage.Question:
                    this.help.Visibility = Visibility.Visible;
                    break;
                case MessageBoxImage.Stop:
                    this.stop.Visibility = Visibility.Visible;
                    break;
                default:
                    this.info.Visibility = Visibility.Visible;
                    break;
            }

            this.ButtonOk.Visibility        = Visibility.Collapsed;
            this.ButtonCancel.Visibility    = Visibility.Collapsed;

            this.ButtonYes.Visibility   = Visibility.Collapsed;
            this.ButtonNo.Visibility    = Visibility.Collapsed;

            this.ButtonOk.IsDefault     = false;
            this.ButtonCancel.IsCancel  = false;

            this.ButtonYes.IsDefault    = false;
            this.ButtonNo.IsDefault     = false;

            switch (this.MessageButtons)
            {
                case MessageBoxButton.OK:
                    this.ButtonOk.Visibility    = Visibility.Visible;

                    this.ButtonOk.IsDefault     = true;
                    break;
                case MessageBoxButton.OKCancel:
                    this.ButtonOk.Visibility        = Visibility.Visible;
                    this.ButtonCancel.Visibility    = Visibility.Visible;

                    this.ButtonOk.IsDefault     = true;
                    this.ButtonCancel.IsCancel  = true;
                    break;
                case MessageBoxButton.YesNo:
                    this.ButtonYes.Visibility   = Visibility.Visible;
                    this.ButtonNo.Visibility    = Visibility.Visible;

                    this.ButtonYes.IsDefault    = true;
                    this.ButtonNo.IsCancel      = true;
                    break;
                case MessageBoxButton.YesNoCancel:
                    this.ButtonYes.Visibility       = Visibility.Visible;
                    this.ButtonNo.Visibility        = Visibility.Visible;
                    this.ButtonCancel.Visibility    = Visibility.Visible;

                    this.ButtonYes.IsDefault    = true;

                    this.ButtonNo.IsCancel      = false;
                    this.ButtonCancel.IsCancel  = true;
                    break;
            }

            this.RaiseEvent(new RoutedEventArgs(InvokeMessageEvent));
        }

        private void    ButtonYes_Click                 (object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(InvokeDialogConfirmEvent));
        }
        private void    ButtonNo_Click                  (object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(InvokeDialogConfirmEvent));
        }
        private void    ButtonOk_Click                  (object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(InvokeDialogConfirmEvent));
        }
        private void    ButtonCancel_Click              (object sender, RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(InvokeDialogConfirmEvent));
        }
	}
}